const Router = require('koa-router');
const query = require('../util/mysql');
const wrapper = require('../util/wrapper');
const checkError = require('../util/checkError');

const router = new Router();

router.prefix('/api/member');

module.exports = router;

/**
 * 获取所有用户
 */
router.get('/all', async (ctx, next) => {
  const res = await query('select * from user');
  if (checkError(res)) {
    ctx.body = await wrapper(new Error('服务异常.'));
    return false;
  }
  ctx.body = await wrapper(res);
});

/**
 * 修改投资信息
 */
router.post('/info/edit', async (ctx, next) => {
  const { id, info } = ctx.request.body;
  if (!id || !info) {
    ctx.body = await wrapper(new Error('参数有误.'));
    return false;
  }
  const res = await query(`update user set info='${info}' where id=${id}`);
  if (checkError(res)) {
    ctx.body = await wrapper(new Error('服务异常.'));
    return false;
  }
  ctx.body = await wrapper('修改成功');
});

router.post('/add', async (ctx, next) => {
  const { name,phone } = ctx.request.body;
  if (!name || !phone) {
    ctx.body = await wrapper(new Error('参数有误.'));
    return false;
  }
  const res = await query();
  if (checkError(res)) {
    ctx.body = await wrapper(new Error('服务异常.'));
    return false;
  }
  ctx.body = await wrapper('注册成功');
});
